package com.easylive.admin.interceptor;

import com.easylive.component.RedisComponent;
import com.easylive.dto.TokenUserInfoDto;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.enums.ResponseCodeEnum;
import com.easylive.exception.BusinessException;
import com.easylive.utils.StringTools;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author 71740
 * @Description TODO
 * @Date 2025/4/20 12:19
 * @Version 1.0
 */
@Component
public class AppInterceptor implements HandlerInterceptor {

    public static final String URL_ACCOUNT = "/account";

    public static final String URL_FILE = "/file";

    @Resource
    private RedisComponent redisComponent;

    /**
     * 预处理请求，决定是否继续处理该请求。
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 如果处理器为空，直接中断请求
        if (null == handler) {
            return false;
        }

        // 如果处理器不是HandlerMethod的实例，继续处理请求
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        // 如果请求的URI包含"account"，继续处理请求
        if (request.getRequestURI().contains(URL_ACCOUNT)) {
            return true;
        }

        //如果不是登陆则要判断是否有token，没有token则说明没有登陆，返回未登录，让前端跳转到登录页面
        String token = request.getHeader(Constants.TOKEN_ADMIN);
        //如果是拿图片请求
        if(request.getRequestURI().contains(URL_FILE)) {
            //没有回返回null
            token = getTokenFromCookies(request);
        }
        if(StringTools.isEmpty(token)) {
            //则去登录页面
            throw new BusinessException(ResponseCodeEnum.CODE_901);
        }
        String tokenInfo4Admin = redisComponent.getTokenInfo4Admin(token);
        if(null == tokenInfo4Admin) {
            throw new BusinessException(ResponseCodeEnum.CODE_901);
        }
        // 默认情况下继续处理请求
        return true;
    }

    private String getTokenFromCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if(cookies!=null) {
            for (Cookie cookie : cookies) {
                if (Constants.TOKEN_ADMIN.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
